import com.alibaba.alimonitor.jmonitor.utils.StringUtils
import com.alibaba.moco.RedisPoolInstance
import com.alibaba.moco.util.BaseTypeUtil
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.sql.Sql
import redis.clients.jedis.Jedis
import script.moco.dto.*
import script.v1.didiCallback.NoticeCallback
import script.v1.didiCallback.NotifyRequest;

/**
 * 关闭所有订单
 * Created by wb-lj240895 on 2016/11/29.
 */
def excute(request) {
    def params = request.getParameterMap();
    CloseAllOrderRequest req = BaseTypeUtil.mapToBean(params, CloseAllOrderRequest.class) as CloseAllOrderRequest

    Jedis redis = RedisPoolInstance.getRedisClient();
    def slurper = new JsonSlurper();

    def db_url = "jdbc:mysql://tddl.daily.alibaba.net:3306/CIO_HAPPYTRIP_APP"
    def username = "CIO_HAPPYTRIP_APP"
    def password = "bnnybqok"
    def driverClass = "com.mysql.jdbc.Driver"
    def sql = Sql.newInstance(db_url, username, password, driverClass)

    def user_id = req.getUser_id()
    def result = [:]
    result.put("user_id", user_id)

    def orderids = []
    sql.eachRow("""
            SELECT t.id,t.order_id,t.order_status,t.user_id,t.passenger_name
            \tFROM ht_taxi_order t
            \tWHERE t.user_id=${user_id}
            \t\tAND t.order_status IN ('0','100','2','300','5','400','401','40','7','50','701')
            """) { row ->
        println("${row.id},${row.order_id},${row.order_status},${row.user_id},${row.passenger_name}")
        def jsonStr = redis.get("[DIDI.ORDER]-" + row.order_id)
        if (null == jsonStr) {
            jsonStr = "{\"order\":{\"tlat\":120.036398,\"extra_info\":\"1\",\"clng\":30.290092,\"require_level\":\"600\",\"end_address\":\"\\u897f\\u6eaa\\u5317\\u82d1\\u5317\\u533a\",\"normal_distance\":null,\"strive_time\":null,\"sub_status\":6001,\"start_name\":\"\\u676d\\u5dde\\u963f\\u91cc\\u5df4\\u5df4\\u897f\\u6eaa\\u56ed\\u533a\\u5317\\u4e00\\u95e8\",\"driver_phone\":\"123456789123\",\"end_name\":\"\\u897f\\u6eaa\\u5317\\u82d1\\u5317\\u533a\",\"begin_charge_time\":null,\"tlng\":30.290092,\"driver_num\":0,\"id\":\"${row.order_id}\",\"driver_name\":\"\\u5218\\u5907\",\"start_address\":\"\\u676d\\u5dde\\u963f\\u91cc\\u5df4\\u5df4\\u897f\\u6eaa\\u56ed\\u533a\\u5317\\u4e00\\u95e8\",\"departure_time\":null,\"driver_avatar\":null,\"flat\":120.023356,\"driver_card\":\"\\u6caaA12345\",\"city\":\"5\",\"driver_order_count\":null,\"driver_level\":\"5\",\"clat\":120.036398,\"driver_car_type\":\"\\u5feb\\u8f66\",\"type\":0,\"passenger_phone\":\"17006428033\",\"finish_time\":null,\"status\":700,\"dlng\":30.290775,\"dlat\":120.038477,\"flng\":30.281692,\"normal_time\":null,\"strive_level\":null,\"delay_time_start\":null},\"price\":{\"detail\":[{\"amount\":\"156.03\",\"name\":\"\\u6b63\\u5e38\\u884c\\u9a76\\u8d39\\u7528\\\"\\u6216\\\"\\u8d85\\u51fa\\u5957\\u9910\\u90e8\\u5206\\u884c\\u9a76\\u8ddd\\u79bb\\u8d39\",\"type\":\"normal_fee\"},{\"amount\":\"10\",\"name\":\"\\u9ad8\\u901f\\u8d39\",\"type\":\"highway_fee\"},{\"type\":\"refund_price\",\"amount\":\"156.0\",\"name\":\"\\u9000\\u6b3e\\u91d1\\u989d\"}],\"total_price\":\"3\"}}"
            println("${row.order_id}订单数据在缓存中不存在  构造数据")
            //redis.set("[DIDI.ORDER]-"+row.order_id, jsonStr)
        }
        def detailMap = slurper.parseText(jsonStr);
        def jsonMap = detailMap.get("order");
        def taxiObj = BaseTypeUtil.jsonToBean(jsonMap, OrderInfo.class) as OrderInfo;//将json对象转换为TaxiRequest对象
        //客服关单完善数据
        taxiObj.setStatus(600);
        taxiObj.setSub_status(6103);

        detailMap.put("order", taxiObj);

        redis.set(GlobalValues.KEY_ORDER + taxiObj.getId(), JsonOutput.toJson(detailMap).toString())    //直接覆盖原有数据，进行修改

        //客服退款完成后进行回调
        //通知回调
        NoticeCallback noticeCallbackTool = new NoticeCallback();
        NotifyRequest notifyRequest = new NotifyRequest();
        notifyRequest.setOrderId(taxiObj.getId());
        notifyRequest.setNotifyType(6);
        notifyRequest.setNotifyDesc("客服关单通知：由于各种原因，客服核实后，关闭订单，通知接入方获取订单详情");
        String url = noticeCallbackTool.getCallbackUrlFromRequest(data)
        if (StringUtils.isNotBlank(url)) {
            noticeCallbackTool.handNotify(url, notifyRequest);
        }

        println(jsonStr)
        orderids.add(taxiObj.getId())
    }
    result.put("closeOrders", orderids)
    result.put("success", "true")
    return result
}

class CloseAllOrderRequest {
    String user_id;
}